package com.system.healthproject.model.doctor.controller;

import com.system.healthproject.annotation.RequireToken;
import com.system.healthproject.bean.*;

import com.system.healthproject.common.ApiResult;

import com.system.healthproject.model.doctor.service.DoctorPatientActivityService;
import com.system.healthproject.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author Xuheng Zhou
 * @date 2020/6/10 20:53
 * @desc
 */
@Controller
@RequestMapping("/doctor/patientActivity")
@Api(value = "医生端-患教模块",tags = "医生端-患教管理API")
public class DoctorPatientActivityController {

    @Autowired
    private DoctorPatientActivityService service;
    @Autowired
    RedisUtil redisUtil;

    @RequireToken
    @ApiOperation(value = "查询医生的患教信息接口(已调试)",notes = "查询医生的患教信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorPatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页数",dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "页数显示数据条数",dataType = "Integer")})
    public ApiResult<PagesBean<DoctorPatientActivityBean>> selectDoctorPatientActivity(@RequestHeader("token") String token,
                                                                                       @RequestParam(name = "page") Integer page ,
                                                                                       @RequestParam(name = "size") Integer size){

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

     //通过loginId获取医生id
        Integer doctorId =service.selectDoctorIdByLoginId(loginId);

        List<DoctorPatientActivityBean> list=service.selectDoctorPatientActivity(doctorId,page,size);

        PagesBean pagesBean=new PagesBean();
        pagesBean.setData(list);
        pagesBean.setPageSize(size);
        pagesBean.setNowsPage(page);
        return ApiResult.successWithData("查询成功",pagesBean);
    }


    /**
     * 查询患教详情信息
     * @param patientactivityId
     * @return
     */
    @ApiOperation(value = "医生查询患教详情接口（已调试）",notes = "查询医生患教详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorOnePatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "patientactivityId",value = "患教活动id",dataType = "String",required = true)
    public ApiResult<DoctorOnePatientActivityBean> selectDoctorOnePatientActivity(@RequestParam Integer patientactivityId){

        System.out.println(patientactivityId);


        DoctorOnePatientActivityBean doctorOnePatientActivityBean=new DoctorOnePatientActivityBean();

        doctorOnePatientActivityBean=service.selectDoctorOnePatientActivity(patientactivityId);

        return ApiResult.successWithData("查询成功",doctorOnePatientActivityBean);
    }

    /**
     * 新增患教信息
     * @param addPatientActivity
     * @return
     */
    @ApiOperation(value = "患教信息新增接口（已调试）",notes = "新增患教信息",httpMethod = "post")
    @RequestMapping(value = "/insertPatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @RequireToken
    public ApiResult insertPatientActivity(@RequestBody DoctorAddPatientActivity addPatientActivity,
                                           @RequestHeader("token") String token) {

        System.out.println(addPatientActivity);

        //获取token中的医生id
        Map<String,Object> map= (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) map.get("loginId");

        //通过loginId获取医生id
        Integer doctorId =service.selectDoctorIdByLoginId(loginId);

        addPatientActivity.setDoctorId(doctorId);
        boolean tf=service.insertPatientActivity(addPatientActivity);
        ApiResult apiResult=new ApiResult();
        if(tf){
            apiResult.setMsg("添加成功");
        }
        return apiResult;

    }

    /**
     * 取消患教活动
     * @param patientactivityId
     * @param cancelreason
     * @return
     */
    @ApiOperation(value = "医生取消患教活动接口（已调试）",notes = "医生取消患教信息",httpMethod = "post")
    @RequestMapping(value = "/doctorCancelPatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientactivityId",value = "患教活动id",dataType = "String",required = true),
            @ApiImplicitParam(name = "cancelreason",value = "取消理由",dataType = "String")
    })
    public ApiResult doctorCancelPatientActivity(@RequestParam(name = "patientactivityId") Integer patientactivityId,
                                           @RequestParam(name = "cancelreason") String cancelreason) {

        System.out.println(cancelreason+"---"+patientactivityId);
        //修改,取消理由，和状态为 已取消1
        boolean tf=service.cancelPatientActivity(patientactivityId,cancelreason);
        ApiResult apiResult=new ApiResult();
        if (tf){
            apiResult.setMsg("取消成功");
        }
        return apiResult;

    }

    /**
     * 结束患教活动
     * @param patientactivityId
     * @return
     */
    @ApiOperation(value = "医生结束患教活动接口（已调试）",notes = "医生结束患教信息",httpMethod = "post")
    @RequestMapping(value = "/doctorClosePatientActivity",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @RequireToken
    @ApiImplicitParam(name = "patientactivityId",value = "患教活动id",dataType = "String",required = true)
    public ApiResult doctorClosePatientActivity(@RequestHeader("token") String token,
            @RequestParam(name = "patientactivityId") Integer patientactivityId) {

        System.out.println("---"+patientactivityId);

        //获取token中的医生id
        Map<String,Object> map= (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) map.get("loginId");

        //结束，把直播结束，添加结束时间，然后修改状态为已结束，同时再获取观看人数，
        //并把活动核桃币平分给每个观看的患者，每个患者的核桃币均增加
        boolean tf=service.doctorClosePatientActivity(patientactivityId,loginId);
        ApiResult apiResult=new ApiResult();
        if (tf){
            apiResult.setCode(200);
            apiResult.setMsg("结束成功");
        }else {
            apiResult.setCode(500);
            apiResult.setMsg("结束失败！可能是活动id输入错误，或有活动用户未开账户，无法进行交易");
        }
        return apiResult;

    }
}
